package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PropertyBasicsInfo;
import com.ruoyi.system.mapper.PropertyBasicsInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PropertyLeaseInfoMapper;
import com.ruoyi.system.domain.PropertyLeaseInfo;
import com.ruoyi.system.service.IPropertyLeaseInfoService;

/**
 * 资产租借记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-08
 */
@Service
public class PropertyLeaseInfoServiceImpl implements IPropertyLeaseInfoService 
{
    @Autowired
    private PropertyLeaseInfoMapper propertyLeaseInfoMapper;
    @Autowired
    private PropertyBasicsInfoServiceImpl propertyBasicsInfoService;

    /**
     * 查询资产租借记录
     * 
     * @param id 资产租借记录主键
     * @return 资产租借记录
     */
    @Override
    public PropertyLeaseInfo selectPropertyLeaseInfoById(Long id)
    {
        return propertyLeaseInfoMapper.selectPropertyLeaseInfoById(id);
    }

    /**
     * 查询资产租借记录列表
     * 
     * @param propertyLeaseInfo 资产租借记录
     * @return 资产租借记录
     */
    @Override
    public List<PropertyLeaseInfo> selectPropertyLeaseInfoList(PropertyLeaseInfo propertyLeaseInfo)
    {

        return propertyLeaseInfoMapper.selectPropertyLeaseInfoList(propertyLeaseInfo);
    }

    /**
     * 新增资产租借记录
     * 
     * @param propertyLeaseInfo 资产租借记录
     * @return 结果
     */
    @Override
    public int insertPropertyLeaseInfo(PropertyLeaseInfo propertyLeaseInfo)
    {
        if (propertyLeaseInfo.getBasicsId() == null){
            throw new ServiceException("请选择资产信息", 500);
        }
        PropertyBasicsInfo propertyBasicsInfo = propertyBasicsInfoService.selectPropertyBasicsInfoById(propertyLeaseInfo.getBasicsId());
        if (propertyBasicsInfo == null){
            throw new ServiceException("资产信息不存在", 500);
        }
        propertyBasicsInfo.setIsLoan("Y");
        propertyBasicsInfoService.updatePropertyBasicsInfo(propertyBasicsInfo);
        propertyLeaseInfo.setCreateTime(DateUtils.getNowDate());
        return propertyLeaseInfoMapper.insertPropertyLeaseInfo(propertyLeaseInfo);
    }

    /**
     * 修改资产租借记录
     * 
     * @param propertyLeaseInfo 资产租借记录
     * @return 结果
     */
    @Override
    public int updatePropertyLeaseInfo(PropertyLeaseInfo propertyLeaseInfo)
    {
        propertyLeaseInfo.setUpdateTime(DateUtils.getNowDate());
        return propertyLeaseInfoMapper.updatePropertyLeaseInfo(propertyLeaseInfo);
    }


    /**
     * 归还
     *
     * @param id 归还
     * @return 结果
     */
    @Override
    public int returnLeaseInfo(Long id)
    {
        PropertyLeaseInfo propertyLeaseInfo = this.selectPropertyLeaseInfoById(id);
        PropertyBasicsInfo propertyBasicsInfo = propertyBasicsInfoService.selectPropertyBasicsInfoById(propertyLeaseInfo.getBasicsId());
        if (propertyBasicsInfo == null){
            throw new ServiceException("资产信息不存在", 500);
        }
        propertyBasicsInfo.setIsLoan("N");
        propertyBasicsInfoService.updatePropertyBasicsInfo(propertyBasicsInfo);
        propertyLeaseInfo.setReturnTime(DateUtils.getNowDate());
        propertyLeaseInfo.setUpdateTime(DateUtils.getNowDate());
        propertyLeaseInfoMapper.updatePropertyLeaseInfo(propertyLeaseInfo);
        return propertyLeaseInfoMapper.deletePropertyLeaseInfoById(id);
    }

    /**
     * 批量删除资产租借记录
     * 
     * @param ids 需要删除的资产租借记录主键
     * @return 结果
     */
    @Override
    public int deletePropertyLeaseInfoByIds(Long[] ids)
    {
        return propertyLeaseInfoMapper.deletePropertyLeaseInfoByIds(ids);
    }

    /**
     * 删除资产租借记录信息
     * 
     * @param id 资产租借记录主键
     * @return 结果
     */
    @Override
    public int deletePropertyLeaseInfoById(Long id)
    {
        return propertyLeaseInfoMapper.deletePropertyLeaseInfoById(id);
    }
}
